<?php

namespace StudyBuddy;

/**
 * Parser of tags of one question
 */
class TagsTokenizer extends \StudyBuddy\String\Tokenizer {

    /**
     * Factory method. This class should be instantiated
     * ONLY through this method
     *
     * @param object of type Utf8string $Tags
     * @return object of this class
     */
    public static function factory(Utf8String $Tags) {

        $str = $Tags->toLowerCase()->trim()->valueOf();

        //$fp = fopen('/var/www/html/qa/www/php.log', 'a+');
        //fwrite($fp, print_r($str,1));
        //fwrite($fp, print_r(new self($str,"/([,]+)/"),1));		
        //fclose($fp);

        return new self($str, '/([,]+)/');
    }

    /**
     * Parse title of the question by
     * tokenizing it
     * Overrides parent's parse and users mb_split
     * instead of preg_split to be UTF-8 Safe
     * because title can be a UTF-8 string
     *
     * @return array tokens;
     */
    public function parse() {
        if (empty($this->origString)) {
            return array();
        }

        /**
         * Remove <> brackets and forward slash. 
         * These are really bad news and will cause
         * alot of headache later, especially the /
         * which will break the url rewrite rules
         * This is important because values of tags are 
         * used in urls
         * 
         * These and should not make into the array
         * str_replace is UTF-8 safe and faster than regex
         * 
         * 
         */
        // 1st param used to be this array: array('<', '>', '/')
        $tokens = str_replace('/', '', $this->origString);

        \mb_regex_encoding('UTF-8');
        $aTokens = \mb_split('[,]+', $tokens);
        d('$aTokens: ' . print_r($aTokens, 1));
        $aTokens = \array_unique($aTokens);
        $aStopwords = \StudyBuddy\getStopwords();

        /**
         * Tags are stored with htmlspecialchars() encoded!
         * This means if searching for tags we need
         * to also run search params through htmlspecialchars()
         * But no extra effort is needed to display tags on html page
         * they will always display as html tags not part of html!
         * this is safe even for using values
         * of tags in xml feed! Very important!
         * 
         */
        array_walk($aTokens, function(&$val) use($aStopwords) {
                    $val = trim($val);
                    $val = ((strlen($val) > 1) && !in_array($val, $aStopwords)) ? htmlspecialchars($val, ENT_QUOTES, 'UTF-8') : false;
                });


        /**
         * One more time
         * just to be 101% sure to Remove empty values
         * because empty values in tags are really
         * bad news for many other functions
         * of this program
         *
         */
        $aTokens = \array_filter($aTokens);
        \sort($aTokens, SORT_STRING);
        d('$aTokens: ' . print_r($aTokens, 1));

        return $aTokens;
    }

}
